/*
 * Copyright 2013 Wajdi Hh "wajdihh@gmail.com".
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hh.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.hh.droid.R;
import com.hh.listeners.OnUiDialogClickListener;

/**
 * 
 * A dialog showing a Title, one, two or three buttons, Icon  and an optional text message with holo light style
 * 
 * @author WajdiHh : Last modification : 23/05/2013
 *
 */
public class UiAlertDialog extends Dialog implements android.view.View.OnClickListener {

	private TextView _myTVTitle, _myTVMessage;
	private Button _myButValidate, _myButCancel, _myButNeutral;
	private LinearLayout _mContainerParent;
	private LinearLayout _mContainerSingleButton;	
	private LinearLayout _mContainerDoubleButton;
	private LinearLayout _mContainerThirdButton;
	private FrameLayout _mSepTitle;
	private int _mNbrLayout;
	private boolean _mIsNegativelButton;
	private String _mTxtNegative;
	private String _mTxtPositive;
	private String _mTxtNeutral;
	private OnUiDialogClickListener _mListenerNegtaive;
	private OnUiDialogClickListener _mListenerNeutral;
	private OnUiDialogClickListener _mListenerPositive;

	/**
	 * 
	 * A dialog showing a Title, one, two or three buttons, Icon  and an optional text message with holo light style
	 * 
	 * @author WajdiHh : Last modification : 23/05/2013
	 *
	 */
	public UiAlertDialog(Context context) {
		super(context);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ui_alertdialog);

		_myTVTitle = (TextView) findViewById(R.id.uiAlertDialog_TvTitle);
		_myTVMessage = (TextView) findViewById(R.id.uiAlertDialog_TvMessage);

		_mContainerSingleButton = (LinearLayout) findViewById(R.id.uiAlertDialog_LlSingelButton);
		_mContainerDoubleButton = (LinearLayout) findViewById(R.id.uiAlertDialog_LlDoubleButton);
		_mContainerThirdButton = (LinearLayout) findViewById(R.id.uiAlertDialog_LlThirdButton);
		_mContainerParent = (LinearLayout) findViewById(R.id.uiAlertDialog_Parent);
		_mSepTitle = (FrameLayout) findViewById(R.id.uiAlertDialog_SepartorTitle);
		_mNbrLayout=0;
		_mTxtNegative="";
		_mTxtPositive="";
		_mTxtNeutral="";
	}

	/**
	 * <br>
	 * Set a listener to be invoked when the negative button (Left side) of the dialog is pressed.
	 * 
	 * @author WajdiHh
	 * @param pText :Text to display on the button
	 * @param pOnClickListener : the listener to handle click
	 * @see OnUiDialogClickListener
	 */
	public void setUiNegativeButton(String pText,OnUiDialogClickListener pOnClickListener) {

		_mIsNegativelButton=true;
		_mNbrLayout++;
		_mTxtNegative=pText;
		_mListenerNegtaive=pOnClickListener;
	}
	/**
	 * <br>
	 * Set a listener to be invoked when the neutral button (the middle) of the dialog is pressed..
	 * 
	 * @author WajdiHh
	 * @param pText :Text to display on the button
	 * @param pOnClickListener : the listener to handle click
	 * @see OnUiDialogClickListener
	 */
	public void setUiPositiveButton(String pText,OnUiDialogClickListener pOnClickListener)  {

		_mNbrLayout++;
		_mTxtPositive=pText;
		_mListenerPositive=pOnClickListener;
	}
	/**
	 * <br>
	 * Set a listener to be invoked when the positive button (right side) of the dialog is pressed.
	 * 
	 * @author WajdiHh
	 * @param pText :Text to display on the button
	 * @param pOnClickListener : the listener to handle click
	 * @see OnUiDialogClickListener
	 */
	public void setUiNeutralButton(String pText,OnUiDialogClickListener pOnClickListener){

		_mNbrLayout++;
		_mTxtNeutral=pText;
		_mListenerNeutral=pOnClickListener;
	}

	/**
	 * <br>
	 * Set the title displayed in the Dialog
	 * 
	 * @author WajdiHh
	 * @param pTitle :String title
	 */
	public void setUiTitle(String pTitle) {
		_myTVTitle.setText(pTitle);
	}
	/**
	 * <br>
	 * Set the message displayed in the Dialog.
	 * 
	 * @param pMessage: String message
	 */
	public void setUiMessage(String pMessage) {
		_myTVMessage.setText(pMessage);
	}
	/**
	 * <br>
	 * Set the text size of the message.
	 * 
	 * @author WajdiHh
	 * @param pTextSize: text size
	 */
	public void setUiMessageTextSize(float pTextSize) {
		_myTVMessage.setTextSize(pTextSize);
	}
	/**
	 * <br>
	 * Set the text size of the title
	 * 
	 * @author WajdiHh
	 * @param pTextSize: text size
	 */
	public void setUiTitleTextSize(float pTextSize) {
		_myTVTitle.setTextSize(pTextSize);
	}

	/**
	 * <br>
	 * Set the icon displayed  on the left top side of the dialog
	 * 
	 * @author WajdiHh
	 * @param pResID: the drawable icon resource ID (R.drawable.MYICONE)
	 */
	public void setUiIcon(int pResID){
		Drawable d=getContext().getResources().getDrawable(pResID);
		_myTVTitle.setCompoundDrawablesWithIntrinsicBounds(d, null, null, null);
	}
	/**
	 * <br>
	 * Set the icon displayed  on the left top side of the dialog
	 * 
	 * @author WajdiHh
	 * @param pDrawable: the drawable icon
	 */
	public void setUiIcon(Drawable pDrawable){
		_myTVTitle.setCompoundDrawablesWithIntrinsicBounds(pDrawable, null, null, null);
	}
	/**
	 * <br>
	 * Set the text message color
	 * 
	 * @author WajdiHh
	 * @param pColor: color integer code
	 */
	public void setUiMessageColor(int pColor){
		_myTVMessage.setTextColor(pColor);
	}
	/**
	 * <br>
	 * Set the text title color
	 * 
	 * @author WajdiHh
	 * @param pColor: color integer code
	 */
	public void setUiTitleColor(int pColor){
		_myTVTitle.setTextColor(pColor);
	}
	/**
	 * <br>
	 * Set the background dialog color
	 * 
	 * @author WajdiHh
	 * @param pColor: color integer code
	 */
	public void setUiBackgroundColor(int pColor){
		_mContainerParent.setBackgroundColor(pColor);
	}
	/**
	 * <br>
	 * Set the background dialog drawable
	 * 
	 * @author WajdiHh
	 * @param pColor: color integer code
	 */
	public void setUiBackgroundDrawable(Drawable pDrawable){
		_mContainerParent.setBackgroundDrawable(pDrawable);
	}
	/**
	 * <br>
	 * Set the color of the line which under title dialog
	 * 
	 * @author WajdiHh
	 * @param pColor: color integer code
	 */
	public void setUiBackgroundTitleSeparator(int pColor){
		_mSepTitle.setBackgroundColor(pColor);
	}
	/**
	 * <br>
	 * Set background color of dialog button
	 * 
	 * @author WajdiHh
	 * @param pColor: color integer code
	 */
	public void setUiBackgroundButton(int pColor){
		switch (_mNbrLayout) {
		case 1:
			_mContainerSingleButton.setBackgroundColor(pColor);
			break;
		case 2:
			_mContainerDoubleButton.setBackgroundColor(pColor);
			break;
		case 3:
			_mContainerThirdButton.setBackgroundColor(pColor);
			break;
		}
	}
	private void checkLayout(){

		switch (_mNbrLayout) {
		case 1:
			_mContainerSingleButton.setVisibility(View.VISIBLE);
			_mContainerDoubleButton.setVisibility(View.GONE);
			_mContainerThirdButton.setVisibility(View.GONE);

			if(_mIsNegativelButton){

				_myButCancel=(Button) findViewById(R.id.uiAlertDialog_BtnSingleButton);
				_myButCancel.setText(_mTxtNegative);
				_myButCancel.setOnClickListener(this);

				if(_mListenerNegtaive!=null){
					_myButCancel.setOnClickListener(_mListenerNegtaive);
					_mListenerNegtaive.setDialog(this);
				}

			}else{		
				_myButValidate=(Button) findViewById(R.id.uiAlertDialog_BtnSingleButton);
				_myButValidate.setText(_mTxtPositive);
				_myButValidate.setOnClickListener(this);

				if(_mListenerPositive!=null){
					_myButValidate.setOnClickListener(_mListenerPositive);
					_mListenerPositive.setDialog(this);
				}
			}
			break;
		case 2:
			_mContainerSingleButton.setVisibility(View.GONE);
			_mContainerDoubleButton.setVisibility(View.VISIBLE);
			_mContainerThirdButton.setVisibility(View.GONE);

			_myButCancel=(Button) findViewById(R.id.uiAlertDialog_BtnNegativeButton);
			_myButCancel.setText(_mTxtNegative);
			_myButCancel.setOnClickListener(this);

			if(_mListenerNegtaive!=null){
				_myButCancel.setOnClickListener(_mListenerNegtaive);
				_mListenerNegtaive.setDialog(this);
			}
			_myButValidate=(Button) findViewById(R.id.uiAlertDialog_BtnPositiveButton);
			_myButValidate.setText(_mTxtPositive);
			_myButValidate.setOnClickListener(this);

			if(_mListenerPositive!=null){
				_myButValidate.setOnClickListener(_mListenerPositive);
				_mListenerPositive.setDialog(this);
			}
			break;
		case 3:
			_mContainerSingleButton.setVisibility(View.GONE);
			_mContainerDoubleButton.setVisibility(View.GONE);
			_mContainerThirdButton.setVisibility(View.VISIBLE);

			_myButCancel=(Button) findViewById(R.id.uiAlertDialog_BtnThirdNegativeButton);
			_myButCancel.setText(_mTxtNegative);
			_myButCancel.setOnClickListener(this);

			if(_mListenerNegtaive!=null){
				_myButCancel.setOnClickListener(_mListenerNegtaive);
				_mListenerNegtaive.setDialog(this);
			}
			_myButValidate=(Button) findViewById(R.id.uiAlertDialog_BtnThirdPositiveButton);
			_myButValidate.setText(_mTxtPositive);
			_myButValidate.setOnClickListener(this);

			if(_mListenerPositive!=null){
				_myButValidate.setOnClickListener(_mListenerPositive);
				_mListenerPositive.setDialog(this);
			}
			_myButNeutral=(Button) findViewById(R.id.uiAlertDialog_BtnThirdNEUTRALButton);
			_myButNeutral.setText(_mTxtNeutral);
			_myButNeutral.setOnClickListener(this);

			if(_mListenerNeutral!=null){
				_myButNeutral.setOnClickListener(_mListenerNeutral);
				_mListenerNeutral.setDialog(this);
			}

			break;
		default:
			_mContainerSingleButton.setVisibility(View.GONE);
			_mContainerDoubleButton.setVisibility(View.GONE);
			_mContainerThirdButton.setVisibility(View.VISIBLE);

			_myButCancel=(Button) findViewById(R.id.uiAlertDialog_BtnThirdNegativeButton);
			_myButCancel.setText(_mTxtNegative);
			_myButCancel.setOnClickListener(this);

			if(_mListenerNegtaive!=null){
				_myButCancel.setOnClickListener(_mListenerNegtaive);
				_mListenerNegtaive.setDialog(this);
			}
			_myButValidate=(Button) findViewById(R.id.uiAlertDialog_BtnThirdPositiveButton);
			_myButValidate.setText(_mTxtPositive);
			_myButValidate.setOnClickListener(this);

			if(_mListenerPositive!=null){
				_myButValidate.setOnClickListener(_mListenerPositive);
				_mListenerPositive.setDialog(this);
			}
			_myButNeutral=(Button) findViewById(R.id.uiAlertDialog_BtnThirdNEUTRALButton);
			_myButNeutral.setText(_mTxtNeutral);
			_myButNeutral.setOnClickListener(this);

			if(_mListenerNeutral!=null){
				_myButNeutral.setOnClickListener(_mListenerNeutral);
				_mListenerNeutral.setDialog(this);
			}
			break;
		}
	}

	@Override
	public void show() {
		super.show();	
		checkLayout();
	}
	@Override
	public void onClick(View v) {
		this.dismiss();
	}
}
